﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTicino.BtCommLib.Mappers;
using BTicino.BtCommLib.Attributes;

namespace BTicino.BtCommLib.Test
{
    /// <summary>
    /// This class is used to translate from high level language to BTicino "DIMENSION VALUE"
    /// code parameter and viceversa.
    /// </summary>
    public class MyDimValuesMapper : IMapperArray
    {
        // Dictionary from high level to BTicino low level dimension messages, for Thermo system
        private Dictionary<string, string> dictToLowLevel_CONFIG = new Dictionary<string, string>();
        // Dictionary from BTicino low level to high level dimension messages, for Thermo system
        private Dictionary<string, string> dictToHighLevel_CONFIG = new Dictionary<string, string>();

        public MyDimValuesMapper()
        {

            dictToLowLevel_CONFIG.Add("OFF","4");
            dictToLowLevel_CONFIG.Add("LOCAL_PROTECTION", "5");
            dictToLowLevel_CONFIG.Add("SET_TO_0", "00");
            dictToLowLevel_CONFIG.Add("SET_TO_+1", "01");
            dictToLowLevel_CONFIG.Add("SET_TO_+2", "02");
            dictToLowLevel_CONFIG.Add("SET_TO_+3", "03");
            dictToLowLevel_CONFIG.Add("SET_TO_-1", "11");
            dictToLowLevel_CONFIG.Add("SET_TO_-2", "12");
            dictToLowLevel_CONFIG.Add("SET_TO_-3", "13");

            dictToHighLevel_CONFIG.Add("4", "OFF");
            dictToHighLevel_CONFIG.Add("5", "LOCAL_PROTECTION");
            dictToHighLevel_CONFIG.Add("00", "SET_TO_0");
            dictToHighLevel_CONFIG.Add("01", "SET_TO_+1");
            dictToHighLevel_CONFIG.Add("02", "SET_TO_+2");
            dictToHighLevel_CONFIG.Add("03", "SET_TO_+3");
            dictToHighLevel_CONFIG.Add("11", "SET_TO_-1");
            dictToHighLevel_CONFIG.Add("12", "SET_TO_-2");
            dictToHighLevel_CONFIG.Add("13", "SET_TO_-3");


        }

        /// <summary>
        /// Traslator from low level messages to high level messages. For specific messages it need the
        /// LowLevelCtxAttr object to discriminate correct value 
        /// </summary>
        /// <param name="key">Key strings, it allows to find high level messages associate to specifi low level message</param>
        /// <param name="ctxAttr">Object contains other frame information (eg. WHO, WHAT, etc...)</param>
        /// <returns>High level messages array</returns>
        public string[] GetValuesToHighLevelArray(string[] keys, LowLevelCtxAttr ctxAttr)
        {
            string[] resultArray = new string[keys.Length];
            string dimension = ctxAttr.GetCtxAttr(BTAttributeName.Dimension);
            string who = ctxAttr.GetCtxAttr(BTAttributeName.Who);
            if (who.Equals(WhoValues.Termo.GetHashCode().ToString()))
            {
                if(dimension.Equals("0"))
                {
                    resultArray[0] = translateTemperature(keys[0]);
                } 
                else if (dimension.Equals("12"))
                {
                    resultArray[0] = translateTemperature(keys[0]);
                }
                else if (dimension.Equals("13"))
                {
                    resultArray[0] = dictToHighLevel_CONFIG[keys[0]];
                }
                else if (dimension.Equals("14"))
                {
                    resultArray[0] = translateTemperature(keys[0]);
                }
            }

            return resultArray;
        }

        /// <summary>
        /// Traslator from high level messages to low level messages. For specific messages it need the
        /// HighLevelCtxAttr object to discriminate correct value 
        /// </summary>
        /// <param name="key">Key strings, it allows to find low level messages associate to specifi high level message</param>
        /// <param name="ctxAttr">Object contains other frame information (eg. WHO, WHAT, etc...)</param>
        /// <returns>Low level messages array</returns>
        private string translateTemperature(string p)
        {
            string rtnValue = string.Empty;
            rtnValue = p.Substring(1, 2) + '.' + p.Substring(3) + '°';
            return rtnValue;
        }

        public string[] GetValuesToLowLevelArray(string[] keys, HighLevelCtxAttr ctxAttr)
        {
            string[] resultArray = new string[keys.Length];

            for (int i = 0; i < keys.Length; i++)
            {
                resultArray[i] = dictToLowLevel_CONFIG[keys[i]];
            }

            return resultArray;
        }
    }
}